import axios from 'axios'
import { setToken, http } from '../../utils'
import { clearToken } from '../../utils/auth'
import store from "../../store"
export const login = (mobile, code) => {
  return async dispatch => {
    try {
      const res = await http.post('/authorizations', {
        mobile,
        code
      })
      setToken(res.token)
      dispatch({ type: 'user/setToken', payload: res.token })
    } catch (error) {
      // 处理登录失败的情况
      console.error('Login failed:', error)
    }
  }
}

export const getUserInfo = () => {
  return async (dispatch) => {
    const data=await http.get('/user/profile')
    dispatch({type:'user/getUserInfo',payload:data.name})
  }
}
export const logout = () => {
  return (dispatch) => {
    clearToken()
    // 清除token和name
    dispatch({ type: 'user/setToken', payload: '' })
    dispatch({ type: 'user/getUserInfo', payload: '' })
  }
}
